import History from './base';
export default class HashHistory extends History {
  constructor (router) {
    super (router);
    ensureSlash (); // 确保默认加个 #/
    console.log ('hash---------');
  }

  // 增加 hash 监听, 根据 当前hash 值来匹配对应组件；
  setupListener () {
    window.addEventListener ('hashchange', () => {
      this.transitionTo (getHash ());
    });
  }
  getCurrentLocation () {
    return getHash ();
  }
  push (location) {
    window.location.hash = location;
  }
}

/**
 * 没有 hash 就加个 #/
 */
function ensureSlash () {
  if (window.location.hash) return;
  window.location.hash = '/';
}

// 获取 # 后面的部分
function getHash () {
  return window.location.hash.slice (1);
}
